// ==UserScript==
// @name           true-friends Custom style switcher
// @description    Add a custom stylesheet switcher to tf.org
// @author         whatevs
// @include        http://true-friends.org/*
// @include        http://*.true-friends.org/*
// @exclude        http://true-friends.org/profile/*
// @exclude        http://*.true-friends.org/profile/*
// @exclude        http://true-friends.org/blogs/*
// @exclude        http://*.true-friends.org/blogs/*
// @exclude        http://true-friends.org/group/*
// @exclude        http://*.true-friends.org/group/*
// @version        1.3
// @updateURL      https://tf-styles.googlecode.com/svn/trunk/global/true-friends-css.user.js
// ==/UserScript==

(function() {
    var oStyles = {"Sandy":"http://tf-styles.googlecode.com/svn/trunk/global/css/tf-custom.css",
                    "Moony":"http://tf-styles.googlecode.com/svn/trunk/global/css/tf-custom2.css",
                	"Mo-light":"http://tf-styles.googlecode.com/svn/trunk/global/css/tf-custom2b.css"};

    var elCss = document.createElement('div');
    var btn = {};
	elCss.id = 'custom_style_switcher';
	for (key in oStyles) {
	   btn = document.createElement('button');
	   btn.innerHTML = key;
	   btn.url = oStyles[key];
	   elCss.appendChild(btn);
	   btn.addEventListener('click', doLink, false);
	}
	document.body.appendChild(elCss);	
	
	s =  readCookie("custom_style");
	if (!s) { s = oStyles["Sandy"]; }
	else { 
	   s = oStyles[s];
	}

	var css = "@import url('" + s + "');";

	css = css + "#custom_style_switcher{position:fixed;top:0;left:0;z-index:1000;background:rgba(0,0,0,0.3);}#custom_style_switcher>button{float:left;clear:both;width:100%;margin:5px;}";
	
	if (typeof GM_addStyle != "undefined") {
		GM_addStyle(css);
	} else if (typeof PRO_addStyle != "undefined") {
		PRO_addStyle(css);
	} else if (typeof addStyle != "undefined") {
		addStyle(css);
	} else {
		var heads = document.getElementsByTagName("head");
		if (heads.length > 0) {
			var node = document.createElement("style");
			node.type = "text/css";
			node.appendChild(document.createTextNode(css));
			heads[0].appendChild(node); 
		}
	}
	
	function doLink(val) {
	   writeCookie("custom_style", this.innerHTML);
	   document.location.reload();
	}
    function writeCookie(cName, cValue) {
        var cExpire = new Date();
        cExpire.setDate(cExpire.getDate() + 30);
        document.cookie = escape(cName) + '=' + escape(cValue) + ';expires=' + cExpire.toUTCString() + ";path=/;domain=true-friends.org";
    }
    function readCookie(cName) {
        if (!cName || !testCookie(cName)) { return null; }
        return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(cName).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
    }
    function testCookie(cName) {
        return (new RegExp("(?:^|;\\s*)" + escape(cName).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
    }
    
})();
